using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Net.Mail;

using CSP.SimpleCMS.Data;
using CSP.MembershipInterface.ObjectModel;

namespace CSP.SimpleCMS.DataModel
{
    /// <summary>Manages the operations related with a Citizen request</summary>  
    public class CitizenRequestDM
    {
        /// <summary>Adds a new citizen request</summary>
        /// <param name="citizenRequest">A CitizenRequest object with the information of the new citizen request to be added</param>
        public static void AddCitizenRequest(CitizenRequest citizenRequest)
        {
            using (DataAccess dataAccess = new DataAccess())
            {
                dataAccess.ExecSP(DMConstants.SPCitizenRequestSAV, new SqlParameter[] {
                    new SqlParameter(DMConstants.FieldUsername, citizenRequest.Username),
                    new SqlParameter(DMConstants.FieldCitizenRequestTypeID, (int)citizenRequest.RequestType),
                    new SqlParameter(DMConstants.FieldFirstName, citizenRequest.FirstName),
                    new SqlParameter(DMConstants.FieldLastName, citizenRequest.LastName),
                    new SqlParameter(DMConstants.FieldPassword, IsNullOrEmpty(citizenRequest.Password)),
                    new SqlParameter(DMConstants.FieldDisplayName, citizenRequest.DisplayName),
                    new SqlParameter(DMConstants.FieldEmailAddress, citizenRequest.EmailAddress),
                    new SqlParameter(DMConstants.FieldDefaultGroup, citizenRequest.DefaultGroup),
                    new SqlParameter(DMConstants.FieldAddress, citizenRequest.Address),
                    new SqlParameter(DMConstants.FieldCity, citizenRequest.City),
                    new SqlParameter(DMConstants.FieldPostalCode, citizenRequest.PostalCode),
                    new SqlParameter(DMConstants.FieldPhone, IsNullOrEmpty(citizenRequest.Phone)),
                    new SqlParameter(DMConstants.FieldChildren, citizenRequest.Children),
                    new SqlParameter(DMConstants.FieldElderlyRelatives, citizenRequest.ElderlyRelatives),
                    new SqlParameter(DMConstants.FieldPhotoUrl, IsNullOrEmpty(citizenRequest.PhotoUrl)) });
            }
        }


        /// <summary>Te civil servant approves the citizen request</summary>
        /// <param name="username">The username of CitizenRequest to be accepted</param>
        public static void ApproveCitizenRequest(string username)
        {
            CitizenRequest citizenRequest = GetCitizenRequestByUsername(username);
            if (citizenRequest != null)
            {
                using (DataAccess dataAccess = new DataAccess())
                {
                    dataAccess.ExecSP(DMConstants.SPCitizenSAV, new SqlParameter[] {
                        new SqlParameter(DMConstants.FieldUsername, citizenRequest.Username),
                        new SqlParameter(DMConstants.FieldFirstName, citizenRequest.FirstName),
                        new SqlParameter(DMConstants.FieldLastName, citizenRequest.LastName),
                        new SqlParameter(DMConstants.FieldDisplayName, citizenRequest.DisplayName),
                        new SqlParameter(DMConstants.FieldEmailAddress, citizenRequest.EmailAddress),
                        new SqlParameter(DMConstants.FieldDefaultGroup, citizenRequest.DefaultGroup),
                        new SqlParameter(DMConstants.FieldAddress, citizenRequest.Address),
                        new SqlParameter(DMConstants.FieldCity, citizenRequest.City),
                        new SqlParameter(DMConstants.FieldPostalCode, citizenRequest.PostalCode),
                        new SqlParameter(DMConstants.FieldPhone, IsNullOrEmpty(citizenRequest.Phone)),
                        new SqlParameter(DMConstants.FieldChildren, citizenRequest.Children),
                        new SqlParameter(DMConstants.FieldElderlyRelatives, citizenRequest.ElderlyRelatives),
                        new SqlParameter(DMConstants.FieldPhotoUrl, IsNullOrEmpty(citizenRequest.PhotoUrl)) });

                    dataAccess.ExecSP(DMConstants.SPCitizenRequestDEL, DMConstants.FieldUsername, username);
                }
            }

        }

        /// <summary>Te civil servant denies the citizen request</summary>
        /// <param name="username">The username of CitizenRequest to be denied</param>       
        public static void DenyCitizenRequest(string username)
        {
            using (DataAccess dataAccess = new DataAccess())
            {
                dataAccess.ExecSP(DMConstants.SPCitizenRequestDEL, DMConstants.FieldUsername, username);
            }
        }

        /// <summary>Returns an array of CitizenRequest with all CitizenRequest stored in the data source</summary>
        /// <returns>An array of CitizenRequest objects with the information of all the CitizenRequest stored in the data source</returns>     
        public static CitizenRequest[] GetCitizenRequests()
        {
            List<CitizenRequest> citizenRequests = new List<CitizenRequest>();

            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCitizenRequestGET, null);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    CitizenRequest citizenRequest = ParseRow(row);
                    citizenRequests.Add(citizenRequest);
                }
            }

            return citizenRequests.ToArray();
        }

        /// <summary>Returns a CitizenRequest with a specified username</summary>
        /// <param name="username">The username of the CitizenRequest to be returned</param>
        /// <returns>The CitizenRequest object with the username</returns>    
        public static CitizenRequest GetCitizenRequestByUsername(string username)
        {
            CitizenRequest citizenRequest = null;

            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCitizenRequestGET, DMConstants.FieldUsername, username);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count == 1)
            {
                citizenRequest = ParseRow(dataSet.Tables[0].Rows[0]);
            }

            return citizenRequest;
        }

        /// <summary>Returns an or a null value depending on the value of the object sent by param</summary>
        /// <param name="caseType">The object to be evaluated</param>
        /// <returns>The object or null value</returns>      
        private static object IsNullOrEmpty(object o)
        {
            if (o == null || (o is string && ((string)o).Length == 0))
                return System.DBNull.Value;
            else
                return o;
        }

        /// <summary>Parses a datarow to a CitizenRequest</summary>
        /// <param name="row">The datarow to be parsed</param>
        /// <returns>A CitizenRequest object with the information stored in the datarow</returns>     
        private static CitizenRequest ParseRow(DataRow row)
        {
            CitizenRequest citizenRequest = new CitizenRequest();

            citizenRequest.Username = row[DMConstants.FieldUsername].ToString();
            citizenRequest.RequestType = (CitizenRequestType)Enum.Parse(typeof(CitizenRequestType), row[DMConstants.FieldCitizenRequestTypeID].ToString());
            citizenRequest.FirstName = row[DMConstants.FieldFirstName].ToString();
            citizenRequest.LastName = row[DMConstants.FieldLastName].ToString();
            if (row[DMConstants.FieldPassword] != System.DBNull.Value)
                citizenRequest.Password = row[DMConstants.FieldPassword].ToString();
            citizenRequest.DisplayName = row[DMConstants.FieldDisplayName].ToString();
            citizenRequest.EmailAddress = row[DMConstants.FieldEmailAddress].ToString();
            citizenRequest.DefaultGroup = row[DMConstants.FieldDefaultGroup].ToString();
            citizenRequest.Address = row[DMConstants.FieldAddress].ToString();
            citizenRequest.City = row[DMConstants.FieldCity].ToString();
            citizenRequest.PostalCode = row[DMConstants.FieldPostalCode].ToString();
            if (row[DMConstants.FieldPhone] != System.DBNull.Value)
                citizenRequest.Phone = row[DMConstants.FieldPhone].ToString();
            citizenRequest.Children = row[DMConstants.FieldChildren].ToString();
            citizenRequest.ElderlyRelatives = row[DMConstants.FieldElderlyRelatives].ToString();
            if (row[DMConstants.FieldPhotoUrl] != System.DBNull.Value)
                citizenRequest.PhotoUrl = row[DMConstants.FieldPhotoUrl].ToString();
            citizenRequest.LastModified = Convert.ToDateTime(row[DMConstants.FieldLastModified]);
            citizenRequest.CreatedDate = Convert.ToDateTime(row[DMConstants.FieldCreatedDate]);

            return citizenRequest;
        }

    }
}
